<template>
  <div id="app">
    <TopBar :key="topBarKey"></TopBar>
    <Header></Header>
    <router-view @updateTopBarKey="topBarKey++" />
    <Footer></Footer>
    <Login v-show="isShowLoginModal"></Login>
    <transition name="slide">
      <Toast v-show="isShowToast"></Toast>
    </transition>
  </div>
</template>

<script>
import TopBar from "@/components/TopBar.vue";
import Header from "@/components/Header.vue";
import Footer from "@/components/Footer.vue";
import Login from "@/components/Login.vue";
import Toast from "@/components/Toast.vue";
import { mapState } from "vuex";
// import axios from "@/request/request";
import { JingpinAPI, UserProfilesAPI } from "@/request/api";

export default {
  components: { TopBar, Header, Footer, Login, Toast },
  data() {
    return { topBarKey: 0 };
  },
  // 映射state
  computed: {
    ...mapState({
      //属性:state=>state.模块名称.属性名
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },

  // 缺点:服务器url地址一样,每个组件调用都要复制 ==>request创建实例+api
  // 每次请求手动编写config ==>拦截器
  // 优化then(),async+await

  async created() {
    // JingpinAPI().then(
    //   (res) => {
    //     console.log("res:" + res.data); //在network查看请求地址,响应结果
    //   },
    //   (err) => {
    //     console.log(err);
    //   }
    // );
    // 使用async+await替换写法
    // 测试获取用户信息
    // let user = await UserProfilesAPI();
    // console.log("用户信息", user);
    // console.log(process.env.VUE_APP_BASE_URL);
    // console.log(process.env.VUE_APP_TXT);
  },
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.topBarKey++;
        }
      },
    },
  },
};
</script>

<style lang="less">
#app {
}
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
